<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="转运计划编号:">
          <el-input v-model="formInline.transferPlanNumber" clearable />
        </el-form-item>
        <el-form-item label="转运申请编号:">
          <el-input v-model="formInline.number" clearable />
        </el-form-item>
        <el-form-item label="产废单位名称:">
          <el-input v-model="formInline.wpcName" clearable />
        </el-form-item>

        <el-form-item label="申请时间:">
          <div class="block">
            <el-date-picker v-model="formInline.transportTime" type="daterange" range-separator="至"
                            start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;" format="yyyy 年 MM 月 dd 日"
            />
          </div>
        </el-form-item>

        <el-form-item label="状态:">
          <el-select v-model="formInline.status" placeholder="请选择" clearable @change="selectedState">
            <el-option v-for="item in transportApplyStateOption" :key="item.value" :label="item.label"
                       :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">

      <!-- 加载动态菜单 -->
      <el-form :inline="true" class="form-inline">
        <el-form-item v-for="item in toolbtn" :key="item.id" >
          <el-button type="primary" :icon='item.meta.icon' @click="showViews(item.name)">
            {{ item.meta.title }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- <el-form :inline="true" class="form-inline">
        <el-button type="primary" icon="el-icon-s-promotion" :disabled="disabled" @click="distribution">
          转运派发
        </el-button>
        <el-button type="primary" icon="el-icon-view" :disabled="disabled" @click="see">
          查看申请
        </el-button>
      </el-form> -->

      <div class="result">
        <el-table ref="multipleTable" v-loading="loading" border :data="tableData" tooltip-effect="dark"
                  style="width: 100%" @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="transferPlanNumber" label="转运计划编号" />
          <el-table-column prop="number" label="转运申请编号" />
          <el-table-column prop="wpcName" label="产废单位名称" width="300" />
          <el-table-column prop="ccName" label="接受单位名称" width="300" />
          <el-table-column prop="wastWeight" label="废物重量 (吨)" />
          <el-table-column prop="type" label="状态" width="120">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center;" :class="'bg-'+scope.row.typeColor" effect="dark">
                {{ scope.row.type }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="申请时间">
            <template slot-scope="scope">
              {{ scope.row.createTime | parseDate }}
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next,sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// Table组件相关逻辑，需要引用
import {
  pageMixin
} from "../../../mixins/index";
import {
  transportApplyState
} from "../../../utils/resource.js";
import {
  parseTime
} from '../../../utils/index.js';
// import {
//   getAllApplyList,
// } from '../../../api/wastecomponey.js';

import {
  getTransportApplyList,
} from '../../../api/wastereceipt.js';
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
    // parseType(value) {
    //   if (value != null) {
    //     console.log("值：" + value);
    //     var obj = transportApplyState.find((p) => p.value === `${value}`);
    //     var text = "";
    //     if (obj) {
    //       text = obj.label;
    //     }
    //     return text;
    //   }
    // },

  },
  mixins: [pageMixin],
  data() {
    return {
      transportApplyStateOption: transportApplyState,
      formInline: {
        transportTime: ["", ""],
        wpcName: "",
        status: "",
        transferPlanNumber: "",
        number: ""
      },
      disabled: true,
      toolbtn: [] // 列表顶部按钮
    };
  },
  async created() {
    this.transportApplyStateOption = transportApplyState.filter(item => item.value !== "0")
    await this.init();
  },
  methods: {
    // 列表顶部功能按钮点击事件
    showViews(action){
      //console.log(action);
      switch(action)
      {
        case '转运派发':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.distribution();
          break;
        case '查看申请':
          if(this.ids.length != 1) {
            this.$message.error('此功能仅限操作1条数据！');
            return;
          }
          this.see();
          break;
      }
    },
    async init() {
      this.tableData = [];
      this.loading = true;
      var searchStatus = "10,20,30,40,50,60";
      // var searchStatus = "";
      if (this.formInline.status) {
        searchStatus = this.formInline.status;
      }
      // const {
      //   data,
      //   count
      // } = await getAllApplyList({
      //   limit: this.pageSize,
      //   page: this.currentPage,
      //   searchParam: JSON.stringify({
      //     Number: this.formInline.number,
      //     WpcName: this.formInline.wpcName,
      //     Type: searchStatus,
      //     StartTime: parseTime(this.formInline.transportTime[0]) === null ? "" : parseTime(this.formInline
      //       .transportTime[0]).substring(0, 10),
      //     EndTime: parseTime(this.formInline.transportTime[1]) === null ? "" : parseTime(this.formInline
      //       .transportTime[1]).substring(0, 10)
      //   })
      // });

      const {
        data,
        count
      } = await getTransportApplyList({
        limit: this.pageSize,
        page: this.currentPage,
        searchParam: JSON.stringify({
          TransferPlanNumber: this.formInline.transferPlanNumber,
          Number: this.formInline.number,
          WpcName: this.formInline.wpcName,
          SeriesId: "eLabR_Middleground",
          Type: searchStatus,
          StartTime: parseTime(this.formInline.transportTime[0]) === null ? "" : parseTime(this.formInline
            .transportTime[0]).substring(0, 10),
          EndTime: parseTime(this.formInline.transportTime[1]) === null ? "" : parseTime(this.formInline
            .transportTime[1]).substring(0, 10),
			 AreaNumber:  this.$store.state.user.userObj.areaNumberStr,
        })
      });

      this.tableData = data;
      this.tableData.forEach(item => {
        var obj = transportApplyState.find((p) => p.value === `${item.type}`);
        //console.log(obj);
        if (obj) {
          item.type = obj.label;
          item.typeColor = obj.color;
        } else {
          item.type = "无数据";
          item.typeColor = "red";
        }
      });
      this.total = count;
      this.loading = false;

      // 初始化按钮数据
      this.toolbtn = JSON.parse(localStorage.getItem('废物申请管理'));
      //console.log(this.toolbtn);
    },

    selectedState(val) {
      console.log(val);
      //this.searchStatus = val;
    },
    async search() {
      await this.init();
    },
    async reset() {
      this.formInline = {
        transportTime: ["", ""],
        wpcName: "",
        status: "",
        transferPlanNumber: "",
        number: ""
      };
      await this.init();
    },
    report() {},
    see() {
      this.$router.push(`/wastereceipt/transformApplyDistribution/${this.id}/detail/${true}`);
    },
    distribution() {
      var obj = this.tableData.find((p) => p.id === this.id);
      console.log(obj)
      if (obj.type !== "待派发") {
        this.$message.error('所选转运申请已派发！');
        return;
      }
      this.$router.push(`/wastereceipt/transformApplyDistribution/${this.id}/distribution/${false}`);
    },
  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .page {
    margin-top: 20px;
    text-align: right;
    margin-bottom: 40px;
  }

  .show-result {
    margin-top: 10px;
  }
</style>
